﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PServiceBus.Core.Runtime.Messages;
using System.Text.RegularExpressions;

namespace PServiceBus.Core.Runtime.Extensions {
    public static class MessageMapperTokenExtension {
        private static readonly Regex _templateRegex =
            new Regex(@"{(?<Key>(.*?))}", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase); 
        public static string TransformTemplate(this MessageMapperToken message, string template) {
            var templateMatches = _templateRegex.Matches(template);
            foreach (Match match in templateMatches) {
                var key = match.Groups["Key"].Value;
                var keyTemplate = String.Concat("{", key, "}");
                template = template.Replace(keyTemplate, message[key]);
            }
            return template;
        }
    }
}
